﻿using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;


namespace FixtureManageSystem.ControlWidget
{
    class ClosableTab: TabItem
    {
        // Constructor
        public ClosableTab()
        {
            // Create an instance of the usercontrol
            CloseableHeader closableTabHeader = new CloseableHeader();

            // assign the usercontrol to the tab header
            this.Header = closableTabHeader;

            // Attach to the CloseableHeader events (Mouse Enter/Leave, Button Click, and Label resize)
            closableTabHeader.button_close.MouseEnter += new MouseEventHandler(Button_close_MouseEnter);
            closableTabHeader.button_close.MouseLeave += new MouseEventHandler(Button_close_MouseLeave);
            closableTabHeader.button_close.Click += new RoutedEventHandler(button_close_Click);
            closableTabHeader.label_TabTitle.SizeChanged += new SizeChangedEventHandler(label_TabTitle_SizeChanged);
        }


        /// <summary>
        /// Property - Set the Title of the Tab
        /// </summary>
        public string Title
        {
            set {

                ((CloseableHeader)this.Header).label_TabTitle.Content = value;
            }
        }


        //
        // - - - Overrides - - -
        //


        // Override OnSelected - Show the Close Button
        protected override void OnSelected(RoutedEventArgs e)
        {
            base.OnSelected(e);
            ((CloseableHeader)this.Header).button_close.Visibility = Visibility.Visible;
        }


        // Override OnUnSelected - Show the Close Button
        protected override void OnUnselected(RoutedEventArgs e)
        {
            base.OnUnselected(e);
            ((CloseableHeader)this.Header).button_close.Visibility = Visibility.Hidden;
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            ((CloseableHeader)this.Header).button_close.Visibility = Visibility.Visible;
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            ((CloseableHeader)this.Header).button_close.Visibility = Visibility.Hidden;
        }



        //
        // ---Event Handlers---
        //


        // Button MouseEnter - When the mouse is over the button - change color to Red
        void Button_close_MouseEnter(object sender, MouseEventArgs e)
        {

            ((CloseableHeader)this.Header).button_close.Foreground = Brushes.Red;
        }

        void Button_close_MouseLeave(object sender, MouseEventArgs e)
        {
            ((CloseableHeader)this.Header).button_close.Foreground = Brushes.Black;
        }


        void button_close_Click(object sender, RoutedEventArgs e)
        {
            ((TabControl)this.Parent).Items.Remove(this);
        }


        void label_TabTitle_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ((CloseableHeader)this.Header).button_close.Margin = new Thickness(((CloseableHeader)this.Header).label_TabTitle.ActualWidth + 5, 3, 4, 0);
        }

    }





}
